﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using ZLCMS.Data.Model;
using System.Text;
using System.Collections;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using System.Reflection;
using NHibernate;
using ZLCMS.Business.Util;


namespace ZLCMS.Web.SystemManage.Basics
{
    public partial class RecruitmentEdit : System.Web.UI.Page
    {
        FrameHelper _frameHelper;

        protected RecruitmentInfo _recrDb;

        protected void Page_Load(object sender, EventArgs e)
        {
            _frameHelper = new FrameHelper(this);
            
            if (!IsPostBack)
            {
                //初始化全局变量值
                InitializeGlobalValue();
                //初始化控件值
                InitializeContent();
            }
            else
            {
                Save();
            }
        }

        private void Save()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                _recrDb = session.Get<RecruitmentInfo>(int.Parse(_id.Text));

                _recrDb.Title = tb_title.Text;
                _recrDb.WorkPost = ddl_pos.SelectedValue;
                _recrDb.WorkAddr = ddl_addr.SelectedValue;
                _recrDb.ExpiryDate = DateTime.Parse(tb_expiry.Text);
                _recrDb.RequstNum = int.Parse(tb_reqnum.Text);
                _recrDb.ShowOrder = int.Parse(tb_order.Text);
                _recrDb.Content = txt_xx.Value;

                session.Update(_recrDb);
                session.Flush();
            }

            _frameHelper.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!",
                navTabId = _frameHelper.NavTabId
            });
        }

        protected void InitializeGlobalValue()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                try
                {
                    if (!IsPostBack)
                    {
                        _id.Text = Request["uid"];
                    }

                    _recrDb = session.Get<RecruitmentInfo>(int.Parse(_id.Text));

                    //TreeDef node = null;
                    //if (_recrDb.TypeId != 0)
                    //{
                    //    node = session.Get<TreeDef>((int)_recrDb.TypeId);
                    //}
                    //if (node != null)
                    //{
                    //    ddTreeControl.SelectedNodeId = node.Id;
                    //    ddTreeControl.SelectedNodeText = node.Name;
                    //}

                    if (_recrDb == null)
                    {
                        throw new Exception("找不到对应信息.");
                    }

                }
                catch (System.Exception ex)
                {
                    _frameHelper.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }
        }

        protected void InitializeContent()
        {
            //页面控件值
            if (!IsPostBack)
            {
                ParamKit.Bind("WorkPos", ddl_pos, null, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, "-1", "-1");
                ParamKit.Bind("WorkCity", ddl_addr, null, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, "-1", "-1");

                tb_title.Text = _recrDb.Title;
                ddl_addr.SelectedValue = _recrDb.WorkAddr;
                ddl_pos.SelectedValue = _recrDb.WorkPost;
                tb_expiry.Text = _recrDb.ExpiryDate.ToString("yyyy-MM-dd");
                tb_reqnum.Text = _recrDb.RequstNum.ToString();
                tb_order.Text = _recrDb.ShowOrder.ToString();
                txt_xx.Value = _recrDb.Content;
            }
        }
    }
}
